package com.patternbox.eclipse.model;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

public class ScenarioInstance {
	
	private static String lookUpString = "<scenario id=\"";
	
	private IFile scenarioFile;
	
	private String type;
	
	public ScenarioInstance(IFile file) {
		scenarioFile = file;
		
		parseId();		
	}
	
	private void parseId() {
		InputStream fileContents;
		try {
			fileContents = scenarioFile.getContents();		
			String content = new Scanner(
					(FileInputStream) fileContents, scenarioFile.getCharset())
					.useDelimiter("\\A").next();
			int startPos = content.indexOf(lookUpString)
					+ lookUpString.length();
			int endPos = content.indexOf("\"", startPos);
			type = content.substring(startPos, endPos);
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getName() {
		return scenarioFile.getName();
	}
	
	public String getType() {
		return type;
	}
	
	public IFile getFile() {
		return scenarioFile;
	}

}
